/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hycreate.platform.modules.crm.business;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.crm.workbench.WorkbenchNextFiled;
import com.hycreate.platform.modules.crm.workbench.WorkbenchService;
import com.hycreate.platform.modules.sys.entity.Area;
import com.hycreate.platform.modules.sys.service.AreaService;
import com.hycreate.platform.modules.sys.utils.UserUtils;

/**
 * 商机录入Controller
 * @author zyc
 * @version 2016-12-03
 */
@Controller
@RequestMapping(value = "${adminPath}/crm/business/customer")
public class CustomerController extends BaseController {

	@Autowired
	private CustomerService customerService;
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private WorkbenchService workbenchService;
	@Autowired
	private AreaService areaService;
	
	@ModelAttribute
	public Customer get(@RequestParam(required=false) String id) {
		Customer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = customerService.get(id);
		}
		if (entity == null){
			entity = new Customer();
		}
		return entity;
	}
	
	/*
	 *进入商机录入页面 
	 */
	@RequiresPermissions("business:customer:view")
	@RequestMapping(value = "form")
	public String form(Customer customer, Model model) {
		//查询所有行业
		CrmSysDict d = new CrmSysDict();
		d.setType("sys_industry");
		List<CrmSysDict> dict = sysDictService.findList(d);
		
		//查询所有省份
		List<Area> provinceArea = areaService.findAllProvince();
		
		model.addAttribute("provinceArea", provinceArea);
		model.addAttribute("customer", customer);
		model.addAttribute("dict", dict);
		return "modules/crm/business/customerForm";
	}
	
	//根据省份查询市区
	@RequestMapping(value = "findCity")
	public void findCity(@RequestParam("parent_id") String parent_id,HttpServletResponse response){
		List<Area> cityArea = areaService.findAllCityByProvince(parent_id);
		
		ObjectMapper om = new ObjectMapper();
		try {
			String cityJson = om.writeValueAsString(cityArea);
			response.setContentType("aplication/json;charset=utf-8");
			response.getWriter().print(cityJson);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//公司信息保存
	@RequiresPermissions("business:customer:edit")
	@RequestMapping(value = "save")
	public String save(Customer customer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, customer)){
			return form(customer, model);
		}
		//customer.setUserid(UserUtils.getUser().getId());
		
		if(customer.getProvince().equals("-1")){
			customer.setProvince(null);
		}
		if(customer.getCity().equals("-1")){
			customer.setCity(null);
		}
		customerService.save(customer);
		addMessage(redirectAttributes, "保存公司信息成功");
		Customer cust = customerService.findMyList(customer.getName());
		//保存到我的收藏客户
		WorkbenchNextFiled wf = new WorkbenchNextFiled(null,null,cust.getId(),UserUtils.getUser().getId());
		workbenchService.addcollect(wf);
		return "redirect:"+Global.getAdminPath()+"/crm/findcustomer/findCustomer/form?id="+cust.getId();
	}
	
	//公司名匹配:判断是否已录入
	@RequiresPermissions("business:customer:edit")
	@RequestMapping(value = "ajax")
	public void ajax(@RequestParam("name") String name,HttpServletResponse response){
		Customer customer = customerService.findMyList(name);
		try {
			response.setContentType("application/text");
			if(null!=customer){
				response.getWriter().print(1);
			}else{
				response.getWriter().print(2);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}